package com.scaffold.chapter.action;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.chapter.entity.Topic;
import com.scaffold.chapter.service.TopicService;

/**
 * Topic操作接口(json)
 * @author wangjg
 */
@Controller
public class TopicJsonAction extends BasicBizAction{
	@Autowired
	private TopicService topicService;

	/**
	 * Topic列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/topic/list.json")
	public String list(String name, String keyword){
		Page page = this.getPage(20);
		List<Topic> list = topicService.pageQuery(page, name, keyword);
		return this.getPageResult(page, list);
	}

	/**
	 * 获取Topic
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/topic/get.json")
	public String get(Long id){
		Topic topic = topicService.getTopic(id);
		this.checkOwner(topic);
		return this.getSuccessResult(topic);
	}

	/**
	 * 保存Topic
	 */
	@RequestMapping("/topic/save.json")
	public String save(Long id){
		if(id==null){
			Topic topic = new Topic();
			this.bindParams(topic);
			this.setOwner(topic);
			this.topicService.save(topic);
		}else{
			Topic topic = this.topicService.getTopic(id);
			this.checkOwner(topic);
			this.bindParams(topic);
			this.topicService.update(topic);
		}
		return this.getSuccessResult();
	}

	/**
	 * 启用/禁用Topic
	 * @param id 编号
	 * @param enabled 是否启用
	 * @return
	 */
	@RequestMapping("/topic/enable.json")
	public String enable(Long id, String enabled){
		if(!"Y".equals(enabled)){
			enabled = "N";
		}
		Topic topic = topicService.getTopic(id);
		this.checkOwner(topic);
		topic.setEnabled(enabled);
		this.topicService.update(topic);
		return this.getSuccessResult(enabled);
	}

	/**
	 * 删除Topic
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/topic/delete.json")
	public String delete(Long id){
		Topic topic = topicService.getTopic(id);
		this.checkOwner(topic);
		this.topicService.delete(topic);
		return this.getSuccessResult();
	}

	/**
	 * 批量删除Topic
	 * @param idseq 编号序列(以‘,’号隔开)
	 * @return
	 */
	@RequestMapping("/topic/batchDelete.json")
	public String batchDelete(String idseq){
		this.topicService.batchDelete(idseq);
		return this.getSuccessResult();
	}

	/**
	 * 获取Topic列表
	 * @param keyword
	 * @return
	 */
	@RequestMapping("/topic/select.json")
	public String select(String name, String keyword){
		List<Topic> list = topicService.pageQuery(null, name, keyword);
		return this.getSuccessResult(list);
	}


}
